<template>
  <div class="device-list-container">
    <search-form
      v-model="searchForm"
      @search="search"
      ref="searchFormComponent"
    ></search-form>
    <div class="device-list-data-table">
      <data-table
        v-model="sort"
        :tableData="tableData"
        @search="search"
        @orderBy="order"
      ></data-table>
    </div>
    <div
      align="right"
      class="paging-footer"
    >
      <el-pagination
        background
        @size-change="pageSizeChangeHandler"
        @current-change="currentPageChangeHandler"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :page-sizes="pageSizes"
        layout="total,sizes, prev, pager, next, jumper"
        :total="totalCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import DataTable from './dataTable';
import moment from 'moment';
import SearchForm from './searchForm';
import { MomentFormat, SortType } from '../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { getUpgradeDeviceListApi } from '../../../api/upgradeDevice';
import { parseError } from '../../../utils';

export default {
  name: 'DeviceList',
  components: {
    SearchForm,
    DataTable
  },
  data() {
    return {
      orderBy: 'createdAt',
      searchForm: {
        btnLoading: false,
        typeUuid: '',
        name: '',
        productNo: '',
        serialNumber: '',
        imsi: '',
        location: '',
        remark: '',
        upgradeStatus: '',
        packageId: '',
        settingId: ''
      },
      sort: {
        sortType: SortType.DESC
      },
      pageSize: 10,
      totalCount: 0,
      tableData: [],
      currentPage: 1,
      pageSizes: [5, 10, 20, 50, 100]
    };
  },
  created() {
    this.search();
  },
  methods: {
    order(value) {
      this.orderBy = value;
    },
    search() {
      this.searchForm.btnLoading = true;
      getUpgradeDeviceListApi(
        this.currentPage - 1,
        this.pageSize,
        this.sort.sortType,
        this.orderBy,
        this.searchForm.typeUuid,
        this.searchForm.name,
        this.searchForm.productNo,
        this.searchForm.serialNumber,
        this.searchForm.imsi,
        this.searchForm.location,
        this.searchForm.remark,
        this.searchForm.upgradeStatus,
        this.searchForm.packageId,
        this.searchForm.settingId
      )
        .then(({ total_count, data }) => {
          this.totalCount = total_count;
          this.tableData = data.map(device => {
            return {
              uuid: device.uuid,
              typeName: device.type_name,
              typeUuid: device.type_uuid,
              name: device.name,
              productNo: device.product_no,
              serialNumber: device.serial_number,
              imsi: device.imsi,
              resetCount: device.reset_count,
              description: device.description,
              version: device.version,
              upgradeStatus: device.upgrade_status,
              settingUpgradeStatus: device.setting_upgrade_status,
              upgradeTotalCount: device.upgrade_total_count,
              packageSucceedCount: device.package_succeed_count,
              packageUpgradeCount: device.package_upgrade_count,
              settingSucceedCount: device.setting_succeed_count,
              settingUpgradeCount: device.setting_upgrade_count,
              createdAt: moment(device.created_at).format(MomentFormat),
              updatedAt: moment(device.updated_at).format(MomentFormat),
              uploadedAt: device.uploaded_at ? moment(device.uploaded_at).format(MomentFormat) : '',
              setting: device.setting,
              package: device.package,
              location: device.location
            };
          });
        })
        .catch(error => Message.error(parseError(error)))
        .finally(() => (this.searchForm.btnLoading = false));
    },
    pageSizeChangeHandler() {
      this.currentPage = 1;
      this.search();
    },
    currentPageChangeHandler() {
      this.search();
    }
  }
};
</script>

<style lang="scss" scoped>
.device-list-container {
  // margin: 10px;
  .el-button--primary {
    background-color: #2c83b1 !important;
    border-color: #2c83b1 !important;
  }
  .device-list-data-table {
    margin-left: 10px;
    margin-right: 10px;
    background-color: #fff;
    padding: 10px;
    margin-top: 10px;
  }
}
.paging-footer {
  margin-left: 10px;
  margin-right: 10px;
  padding: 10px;
  background: #fff;
}
</style>

